<?php

namespace app\admin\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\db\exception\DbException;
use think\Exception;
use think\Db;
use think\exception\PDOException;

class Hello extends Command
{

    private $XM_DIR = '/Users/cody/PhpstormProjects/www/web.man.fa';

    //  本地测试地址
    private $path_front = '/Users/cody/PhpstormProjects/www/web.man.fa/runtime';

    /**
     * 服务器上的站点目录
     * @var string[]
     */
    protected $path = [
        // 盲盒-A站
        0 => '/www/wwwroot/a.site.com',

        // 盲盒-B站
        1 => '/www/wwwroot/template-b1-us-general',

        // 2=LP-A站
        2 => '/www/wwwroot/a.lp.site.com',

        // 4=laravel管理后台,
        4 => '/www/wwwroot/vue.admin.ht',

        //5=laravel店铺后台
        5 => '/www/wwwroot/vue.shop.ht',

        // stripe
        6 => '/www/wwwroot/strip.demo',


        7 => '/www/wwwroot/zeayan.com',
    ];


    protected function configure()
    {
        $this->setName('hello')
                ->setDescription('Compress js and css file');
    }

    //  /usr/local/bin/unrar x -r -o+ /www/wwwroot/a.lp.site.com/9407c477f270957016827f989b43fe72.rar /www/wwwroot/a.lp.site.com
    // /usr/local/bin/unrar /www/wwwroot/a.lp.site.com9407c477f270957016827f989b43fe72.rar
// web.man.test  web.man.test    webmanadmin
    //  站点:0=盲盒-A站,1=盲盒-B站,2=LP-A站,3=LP-B站,4=laravel管理后台,5=laravel店铺后台
    protected function execute(Input $input, Output $output)
    {
//        $this->XM_DIR = '/www/wwwroot/demo.two';
        $this->XM_DIR = '/www/wwwroot/demo.one';

        $this->path_front = '';
        $list = Db::table('ddhd_qd')->where('is_exec', 0)->select();
        if ($list)
        {
            foreach ($list as $row)
            {

                try {
                    $row_file = $row['file'];
                    $row_file = strrchr($row_file, '/');
                    if ($row_file) {
                        $row_file = substr($row_file, 1);
                    }
                    $status = $row['status'];
                    $file = $this->XM_DIR . '/public' . $row['file'];
                    $path = $this->path_front . $this->path[$status];
                    if (!is_dir($path)) {
                        $this->exec("mkdir -p " . $path);
                    }
                    if (file_exists($file)) {

                        // todo  复制
                        $command = 'cp ' . $file . ' ' . $path; // 这里以列出当前目录文件为例
                        $this->exec($command);

                        // todo 删除原有文件，因为vue生成时，随机生成文件名，会造成很多的垃圾文件,同步时需删除之前的旧文件
                        if (file_exists($path.'/dist')) {
                            $command = 'rm -rf '.$path.'/dist/*';
                            $this->exec($command);
                        }
                        
                        // todo 解压
                        $nfile = $path . '/' . $row_file;
                        if (strpos($nfile,'.rar')){
                            $command = '/usr/local/bin/unrar x -r -o+ ' . $nfile . ' ' . $this->path[$status];
                        }elseif (strpos($nfile,'.zip')){
                            $command = 'unzip -o '.$nfile.' -d '.$this->path[$status];
                        }else{
                            $this->TX(__METHOD__."文件格式错误");
                        }
                        $this->exec($command);

                        // todo 权限设置 -1
                        $command = 'chmod -R 755 ' . $this->path[$status] . '/*';
                        $this->exec($command);

                        // todo 权限设置 -2
                        $command = 'chown -R www:www ' . $this->path[$status] . '/*';
                        $this->exec($command);
                        
                        // todo 删除 压缩文件  rar 格式
                        $command = 'rm -rf '.$this->path[$status].'/*.rar';
                        $this->exec($command);
                        $command = 'rm -rf '.$this->path[$status].'/*.zip';
                        $this->exec($command);

                    }
                    $update = Db::table('ddhd_qd')->where('id', $row['id'])->update(['is_exec' => 1]);
                } catch (DbException|Exception $e) {
                    var_dump($e->getMessage());
                    exit();
                }


                if ($update){
                    $this->TX(__METHOD__.'成功');
                }else{
                    $this->TX(__METHOD__.'失败');
                }
            }
        }

    }


    /**
     * 执行命令
     * @param $command
     * @return void
     */
    private function exec($command){
        $this->TX($command);
        $exec = exec($command);
        if ($exec === 'false'){
            dump('指令执行错误 == '.$command);
            exit(200);
        }
    }

    private function TX($msg){

    }
}
